package com.xiaoxiong.gobang.control;

import android.util.Log;

import com.xiaoxiong.gobang.bean.GameState;
import com.xiaoxiong.gobang.bean.Player;

/**
 * @author xiaoxiong
 * @Package com.xiaoxiong.gobang.control
 * @Description:游戏控制器
 * @email 249900679@qq.com
 * @date 2016/4/1 23:51
 */
public class GameControl {
    private GameState m_GameState;
    private Player[] m_players;
    private ChessPosition chessPosition;
    private Thread thread;
    private boolean isStop;

    public GameControl() {
        this.m_players = new Player[3];
    }

    /**
     * 设置下棋控制器
     *
     * @param chessPosition
     */
    public void setChessPosition(ChessPosition chessPosition) {
        this.chessPosition = chessPosition;
    }

    /**
     * 设置玩家
     *
     * @param m_player 玩家
     * @param player   玩家ID
     */
    public void setPlayers(Player m_player, int player) {
        this.m_players[player] = m_player;
    }

    /**
     * 初始化棋盘状态
     *
     * @param gameState
     */
    public void InitGameState(GameState gameState) {
        this.m_GameState = gameState;
        m_players[1].m_state = gameState;
        m_players[2].m_state = gameState;
    }

    /**
     * 通过玩家ID获取玩家
     *
     * @param playerId 玩家ID
     * @return
     */
    private Player GetPlayer(int playerId) {
        return m_players[playerId];
    }

    /**
     * 开始游戏
     */
    public void Run() {

        this.isStop = false;
        Player player = GetPlayer(m_GameState.GetCurrentPlayer());
        if (player.isAi()) {
            m_GameState.PutChess(136, m_GameState.GetCurrentPlayer());
            chessPosition.SetName(player.GetPlayerName());
            Draw(136, m_GameState.GetCurrentPlayer());
            m_GameState.SwitchPlayer();
        }

        thread = new Thread() {
            @Override
            public void run() {
                while (!m_GameState.IsGameOver() && !isStop) {
                    int playid = m_GameState.GetCurrentPlayer();
                    Player currentPlayer = GetPlayer(playid);
                    if (currentPlayer != null) {
                        int np = currentPlayer.GetNextPosition();
                        chessPosition.SetName(currentPlayer.GetPlayerName());
                        m_GameState.PutChess(np, playid);
                        Draw(np, playid);
                        m_GameState.SwitchPlayer();
                    }
                }
                int winner = m_GameState.GetWinner();
                if (winner == Player.NULLPALYER) {
                    chessPosition.SetWinner(null);
                } else {
                    Player player = GetPlayer(winner);
                    chessPosition.SetWinner(player);
                }
            }
        };
        thread.start();
    }

    public boolean isOver() {
        return m_GameState.IsGameOver();
    }

    public String who() {
        Log.d("name", GetPlayer(m_GameState.GetCurrentPlayer()).GetPlayerName() + " 下棋中");
        return GetPlayer(m_GameState.GetCurrentPlayer()).GetPlayerName();
    }

    public void Stop() {
        this.isStop = true;
    }

    /**
     * 绘制棋局
     *
     * @param playerid 玩家ID
     * @param pos      下子位置
     */
    private void Draw(int pos, int playerid) {
//        this.chessPosition.setBoard(board, pos);
        this.chessPosition.setPos(pos, playerid);
    }
}
